﻿//------------------------------------------------------------------
//
//  For licensing information and to get the latest version go to:
//  http://www.codeplex.com/perspective
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY
//  OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
//  LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
//  FITNESS FOR A PARTICULAR PURPOSE.
//
//------------------------------------------------------------------
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using Perspective.Wpf3D.Primitives;
using Perspective.Wpf3D.Sculptors;

namespace Perspective.Wpf3D.Shapes
{
    /// <summary>
    /// 组成圆环的所有点和三角形集合
    /// A 3D ring model.
    /// Default radius is 10.0.
    /// </summary>
    public class Ring3D : PolygonalElement3D
    {
        #region Constuctors
        /// <summary>
        /// Static constructor.
        /// No default back material for 3D closed shapes.
        /// </summary>
        static Ring3D()
        {
            BackMaterialProperty.OverrideMetadata(
                typeof(Ring3D),
                new PropertyMetadata(
                    null,
                    VisualPropertyChanged));
        }
        #endregion

        #region Properties

        #region InnerRadius 旋转半径

        /// <summary>
        /// 旋转半径
        /// Gets or sets the ring InnerRadius.
        /// Default value is 10.0.
        /// </summary>
        public double InnerRadius
        {
            get { return (double)GetValue(InnerRadiusProperty); }
            set { SetValue(InnerRadiusProperty, value); }
        }

        /// <summary>
        /// Identifies the InnerRadius dependency property.
        /// </summary>
        public static readonly DependencyProperty InnerRadiusProperty =
            DependencyProperty.Register(
                "InnerRadius",
                typeof(double),
                typeof(Ring3D),
                new UIPropertyMetadata(
                    10.0, 
                    VisualPropertyChanged),
                    PositiveDoubleValidateValue);

        #endregion

        #region RotatedRadius 旋转圆半径
        /// <summary>
        /// 旋转圆半径
        /// Gets or sets the rotate circle radius.
        /// Default value is 10.0.
        /// </summary>
        public double RotatedRadius
        {
            get { return (double)GetValue(RotatedRadiusProperty); }
            set { SetValue(RotatedRadiusProperty, value); }
        }

        /// <summary>
        /// Identifies the rotate circle radius dependency property.
        /// </summary>
        public static readonly DependencyProperty RotatedRadiusProperty =
            DependencyProperty.Register(
                "RotatedRadius",
                typeof(double),
                typeof(Ring3D),
                new UIPropertyMetadata(
                    1.0, 
                    VisualPropertyChanged),
                    PositiveDoubleValidateValue);
        #endregion

        #region SegmentCount 圆周分段数

        /// <summary>
        /// Gets or sets the ring bar segment count.
        /// Default value is 40. Minimum value is 3.
        /// </summary>
        public int SegmentCount
        {
            get { return (int)GetValue(SegmentCountProperty); }
            set { SetValue(SegmentCountProperty, value); }
        }

        /// <summary>
        /// Identifies the SegmentCount dependency property.
        /// </summary>
        public static readonly DependencyProperty SegmentCountProperty =
            DependencyProperty.Register(
                "SegmentCount",
                typeof(int),
                typeof(Ring3D),
                new UIPropertyMetadata(
                    40, 
                    VisualPropertyChanged),
                    IsValidSegmentCountValue);

        #endregion

        #endregion


        /// <summary>
        /// Validation of the SegmentCount value.
        /// </summary>
        /// <param name="value">Value to test.</param>
        /// <returns>Boolean value.</returns>
        private static bool IsValidSegmentCountValue(object value)
        {
            int i = (int)value;
            return i >= 3;
        }

        // private RingSculptor _sculptor = new RingSculptor();

        /// <summary>
        /// Called by UIElement3D.InvalidateModel() to update the 3D model.
        /// </summary>
        protected override void OnUpdateModel()
        {
            Sculptor = new RingSculptor(InnerRadius, RotatedRadius,SegmentCount, SideCount, InitialAngle, RoundingRate);
            //_sculptor.Initialize(Radius, SegmentCount, SideCount, InitialAngle, RoundingRate);
            //_sculptor.BuildMesh();
            //Geometry = _sculptor.Mesh;
            base.OnUpdateModel();
        }
    }
}
